<?php 
//Luu lai category_id ban dau
if(isset($_REQUEST['hdn_back_cate_id']))
{
	$v_back_cate_id = $_REQUEST['hdn_back_cate_id'];
}
if(isset($_REQUEST['hdn_back_agency_id']))
{
	$v_back_agency_id = $_REQUEST['hdn_back_agency_id'];
}
//Luu lai dieu kien loc
if(isset($_REQUEST['hdn_filter']))
{
	$v_filter = $_REQUEST['hdn_filter'];
}

if(isset($_REQUEST['hdn_method']))
{
	$v_method = $_REQUEST['hdn_method'];
}

$v_accept = $_REQUEST['rad_accept'];

if($v_accept==1){ //Chap nhan duyet
	$v_status = 2;
}else{
	$v_status = 3; //Khong chap nhan
	$v_refuse = trim(_restore_XML_bad_char($_REQUEST['txt_reason']));
}
if ($v_status== 2) {
	$v_approver_id = $_SESSION['staff_id'];
	$v_approve_date = _ddmmyyyy_to_yyyymmdd(date('d/m/Y')); 
}
//Doan nay can them vao de thuc hien cap nhat theo bo DEV-EDITOR
	SetDevEditPath($DEVEDIT_PATH);
	$short_des = new DevEdit();
	$short_des->SetName("txt_detail");
	$v_content = _replace_bad_char($short_des->GetValue(false)); //Thuc hien de cap nhat noi dung
//
//$v_content = $_REQUEST['hdnContent'];
$v_fisrt = intval($_REQUEST['hdn_fisrt']);
$v_cate_id = $_REQUEST['hdn_cate_id'];
$v_agency_id = $_REQUEST['hdn_agency_id'];
$v_creator_id = $_REQUEST['hdn_creator_id']; 
$v_valid_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_valid_date']);
$v_af_id = intval($_REQUEST['hdn_af_id']);
$v_af_code = _replace_bad_char(trim($_REQUEST['txt_af_code']));
$v_af_name = _replace_bad_char($_REQUEST['txt_af_name']);
$v_order = trim($_REQUEST['txt_order']);
$v_deleted_exist_file_id_list = $_REQUEST['hdn_deleted_exist_file_id_list']; 
$v_deleted_new_file_id_list = $_REQUEST['hdn_deleted_new_file_id_list']; 
$v_url = "index.php?modal_dialog_mode=1" . "&hdn_item_id=" . $v_item_id . "&hdn_cate_id=".$v_cate_id."&fuseaction=DISPLAY_SINGLE_AF" ;
$function_code = _CONST_ONEGATE_AF_UPDATE;
//Kiem tra quyen cua NSD
if(!_check_permission_on_function($_SESSION['staff_id'], $_ISA_APP_CODE, $function_code)){?>
	<script>
		alert("<? echo _CONST_FUNCTION_PERMISSION_ERROR; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<? echo $v_url; ?>";
		}else{
			window.history.back();
		}	
	</script>
	<?php
}
$v_new_file_id_list="";
$v_fuseaction_back = "DISPLAY_ALL_AF";	
if(_is_sqlserver()){
	//Du lieu MSSQL
	for($i=0; $i<_CONST_MAX_NUMBER_OF_ATTACH_FILE; $i++){
		$v_form_field = 'file_attach'.$i;
		if (isset($_FILES[$v_form_field]['tmp_name'])){
			if ($v_deleted_new_file_id_list=="" Or _list_have_element($v_deleted_new_file_id_list, $i, ",")==false){
				$v_filename = _replace_bad_char(trim($_FILES[$v_form_field]['name']));
				$v_tmp_filename    = $_FILES[$v_form_field]['tmp_name'];
				$v_file_id = 0;
				if ($v_tmp_filename!="" and !is_null($v_tmp_filename)){
					_copy_file($v_tmp_filename, _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					if ($v_new_file_id_list ==""){
						$v_new_file_id_list = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}else{			
						$v_new_file_id_list = $v_new_file_id_list . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL1_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
					}	
				}	
			}	
		}	
	}
	// Cap nhat noi dung giai nghia	
	$v_text_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_content);
	$sql = "Exec Onegate_AFUpdate " ;
	$sql = $sql . $v_af_id;
	$sql = $sql . ",'" . $v_agency_id . "'";
	$sql = $sql . "," . $v_cate_id;
	$sql = $sql . ",'" . $v_creator_id . "'";
	$sql = $sql . ",'" . $v_approver_id."'";
	$sql = $sql . ",'" . $v_af_code."'" ;
	$sql = $sql . ",'" . $v_af_name . "'";
	$sql = $sql . ",'" .$v_valid_date . "'";
	$sql = $sql . "," . $v_status;
	$sql = $sql . ",'" .$v_approve_date . "'";
	$sql = $sql . "," . $v_order;
	$sql = $sql . ",'" .$v_refuse . "'";
	$sql = $sql . ",'" .$v_new_file_id_list . "'";
	$sql = $sql . ",'" . $v_deleted_exist_file_id_list . "'";
	$sql = $sql . "," . $v_text_id; 
	$result = mssql_query($sql,$conn);
	$rs = mssql_fetch_array($result);
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	@mssql_free_result($result);
}
if (!is_null($v_error) and $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}sleep(0);
?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="<?php echo $v_fuseaction_back; ?>">
	<input name="hdn_filter" type="hidden" value="<?php echo $v_filter; ?>">
	<input name="hdn_af_id" type="hidden" value="<?php echo $v_af_id; ?>">
	<input name="hdn_cate_id" type="hidden" value="<?php echo $v_cate_id; ?>">
	<input name="hdn_back_cate_id" type="hidden" value="<?php echo $v_back_cate_id; ?>">
	<input name="hdn_agency_id" type="hidden" value="<?php echo $v_agency_id; ?>">
	<input name="hdn_back_agency_id" type="hidden" value="<?php echo $v_back_agency_id; ?>">
	<input name="hdn_status" type="hidden" value="<?php echo $v_status; ?>">
	<input name="hdn_refuse" type="hidden" value="<?php echo $v_refuse; ?>">
	<input name="hdn_method" type="hidden" value="<?php echo $v_method; ?>">
	<input name="hdn_accept" type="hidden" value="<?php echo $v_accept; ?>">
	<input name="hdn_deleted_new_file_id_list" type="hidden" value="<?php echo $v_deleted_new_file_id_list;?>">
	<input name="hdn_deleted_exist_file_id_list" type="hidden" value="<?php echo $v_deleted_exist_file_id_list;?>">
</form>
<Script language="javascript">
	document.forms(0).submit();
</Script>
